home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Developer / StopWatch / Source / Expense.m < prev    next >
Encoding:
Text File  |  1994-02-06  |  2.0 KB  |  128 lines

  1. /*
  2.  * For legal stuff see the file COPYRIGHT
  3.  */
  4. #import <stdio.h>
  5. #import <stdlib.h>
  6. #import "Expense.h"
  7.  
  8. extern void freeAndCopy( char **ptr, const char *str );
  9. extern char *NXCopyStringBuffer(const char *buffer);
  10.  
  11. @implementation Expense
  12.  
  13. static ColDesc Layout[] = {
  14.   { 3.0,    "dateString",         (SEL)0 },
  15.   { 62.0,    "amountString",        (SEL)0 },
  16.   { 116.0,    "description",        (SEL)0 },
  17.   { 0.0,    NULL,            (SEL)0 },
  18. };
  19.  
  20. + initialize
  21. {
  22.   ColDesc *ptr;
  23.  
  24.   /* Initialize the selectors in the ColDesc table */ 
  25.   for ( ptr = Layout; ptr->method; ptr++ )
  26.     ptr->selector = sel_getUid( ptr->method );
  27.  
  28.   return self;
  29. }
  30.  
  31. - init:(const char *)date
  32.        description:(const char *)desc
  33.        amount:(const float)amt
  34. {
  35.  
  36.   [self setDateString:date];
  37.   [self setDescription:desc];
  38.   amount = amt;
  39.   return self;
  40. }
  41.  
  42. - (void) freeDesc
  43. {
  44.   if ( description ) {
  45.     free( description );
  46.     description = NULL;
  47.   }
  48. }
  49.  
  50. - free
  51. {
  52.   [self freeDesc];
  53.   return [super free];
  54. }
  55.  
  56. - (ColDesc *)colDesc
  57. {
  58.   return Layout;
  59. }
  60.  
  61. - setAmount:(float)value
  62. {
  63.   amount = value;
  64.   return self;
  65. }
  66.  
  67. - setDateString:(const char *)str
  68. {
  69.   freeAndCopy( &dateString, str );
  70.   return self;
  71. }
  72.  
  73. - setDescription:(const char *)desc
  74. {
  75.   [self freeDesc];
  76.   description = NXCopyStringBuffer(desc);
  77.   return self;
  78. }
  79.  
  80. - (const char *)amountString
  81. {
  82.   static char buf[20];
  83.  
  84.   sprintf( buf, "%.2f", amount );
  85.   return buf;
  86. }
  87.  
  88. - (const char *)dateString
  89. {
  90.   return dateString;
  91. }
  92.  
  93. - (const char *)description
  94. {
  95.   return description;
  96. }
  97.  
  98. - (float)amount
  99. {
  100.   return amount;
  101. }
  102.  
  103. /*
  104.  * For use in comparisons, convert date such as 11/21/93 to a
  105.  * single integer 932111. THIS SHOULD BE CACHED! (TBD)
  106.  */
  107. - (int)dateValue
  108. {
  109.   int day, month, year;
  110.  
  111.   sscanf( dateString, "%d/%d/%d", &month, &day, &year );
  112.   return ( year * 10000 + month * 100 + day );
  113. }
  114.  
  115. - read:(NXTypedStream *) stream
  116. {
  117.   NXReadTypes( stream, "**f", &description, &dateString, &amount );
  118.   return self;
  119. }
  120.  
  121. - write:(NXTypedStream *) stream
  122. {
  123.   NXWriteTypes( stream, "**f", &description, &dateString, &amount );
  124.   return self;
  125. }
  126.  
  127. @end
  128.